package com.yoomiito.app.ui.my.remain.deduct

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import android.view.View
import com.chad.library.adapter.base.BaseQuickAdapter
import com.gyf.barlibrary.ImmersionBar
import com.yoomiito.app.R
import com.yoomiito.app.base.App
import com.yoomiito.app.base.BaseActivity
import com.yoomiito.app.model.BaseList
import com.yoomiito.app.model.my.DeductMoneyDetail
import com.yoomiito.app.utils.DensityUtil
import com.yoomiito.app.widget.SpacesItemDecoration
import com.yoomiito.app.widget.SpacingDecoration
import kotlinx.android.synthetic.main.act_deduct_money.*
import kotlinx.android.synthetic.main.view_my_order_top_remain.*

/**
 * 可抵扣余额
 */
class DeductMoneyActivity :BaseActivity<DeductMoneyPresenter>(){

    private val mAdapter = DeductMoneyDetailAdapter(null)
    private var mCurrentPage = 1
    companion object{
        fun start(context: Context){
            val intent = Intent(context,DeductMoneyActivity::class.java)
            context.startActivity(intent)
        }
    }

    override fun getLayoutId(): Int = R.layout.act_deduct_money

    override fun newP(): DeductMoneyPresenter = DeductMoneyPresenter(App.mAppConfig)

    public override fun initImmersionBar() {
        ImmersionBar.with(this).keyboardEnable(false).init()
    }
    override fun initData(savedInstanceState: Bundle?) {
        initView()
        showLoading()
        requestData()
    }

    private fun initView() {
        back.setOnClickListener { finish() }
        titleTv.text = "可抵扣余额"
        tv_des.text = "可抵扣余额(元)"
        account_detail.visibility = View.INVISIBLE

        smartRefreshLayout.setEnableRefresh(false)
        smartRefreshLayout.setOnLoadMoreListener {
            if (!mAdapter.isLoading){
                mCurrentPage++
                requestData()
            }
        }
        rcy.layoutManager = androidx.recyclerview.widget.LinearLayoutManager(this)
        rcy.addItemDecoration(SpacingDecoration(0,DensityUtil.dp2px(1f),true))
        rcy.adapter = mAdapter
//        mAdapter.setOnLoadMoreListener({
//            if (!mAdapter.isLoading){
//                mCurrentPage++
//                requestData()
//            }
//        },rcy)
    }

    private fun requestData(){
        p?.getData(mCurrentPage)
    }

    fun setData(t: BaseList<DeductMoneyDetail>?){
        dismissLoading()
        tv_money.text = t?.deductionAccount
        if (t?.current_page==1){
            if (t?.data?.size!!>0){
                detailView.visibility = View.VISIBLE
                mAdapter.setNewData(t?.data)
            }else{
                detailView.visibility = View.GONE
            }
        }else{
            mAdapter.addData(t?.data!!)
            smartRefreshLayout.finishLoadMore()
//            mAdapter.loadMoreComplete()
        }
        if (t?.current_page==t?.last_page){
            smartRefreshLayout.finishLoadMoreWithNoMoreData()
//            mAdapter.loadMoreEnd()
        }
        mCurrentPage = t?.current_page
    }
}